﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LightCAD.Runtime
{

    public class DrawingEditRuntime
    {
        private DocumentRuntime docRt { get; set; }


        public IDrawingEditControl Control { get; set; }
        public DrawingEditorType ActiveEditorType { get; set; }
        public ViewportRuntime ActiveViewportRt { get; set; }
        public Dictionary<long, ViewportRuntime> ViewportRts { get; set; }

        public Model3DEditRuntime Model3DEditRt { get; set; }

        
        public DrawingEditRuntime(DocumentRuntime docRt, IDrawingEditControl control)
        {
            this.docRt= docRt;
            this.Control = control;
            this.ActiveEditorType =  DrawingEditorType.Drawing;
            this.ViewportRts = new Dictionary<long, ViewportRuntime>();
        }
        public void InitModel3DEditRuntime()
        {
            if (this.Model3DEditRt != null)
            {
                this.Model3DEditRt.Dispose();
            }
            this.Model3DEditRt = new Model3DEditRuntime(docRt, this.Control.Model3DEditControl);
        }
        internal void Initilize()
        {
            ResetViewportRts();
            Control.RefreshViewports();
        }
        public void ResetViewportRts()
        {
            var vportCtrls = Control.Viewports;
            var layout = docRt.Document.ActiveLayout;
            ViewportRts.Clear();
            ViewportRuntime first=null;
            for (var i = 0; i < layout.Viewports.Length; i++)
            {
                var vport = layout.Viewports[i];
                var vportCtrl = vportCtrls.First((vp)=>vp.ViewportId==vport.Id);
                var vportRt = new ViewportRuntime(docRt, vport, vportCtrl);
                this.ViewportRts.Add(vport.Id,vportRt);
                if (i == 0) first = vportRt;
            }
            this.ActiveViewportRt = first;
        }

       
    }
}
